package org.example;

import java.util.concurrent.locks.ReentrantLock;

/**
 * T1 sleep的时候，T2读到了T1更改的number
 * 输出
 * t2:2  //脏读
 * t1:2
 */
public class DirtyReadDemo {
    private static int number = 1;
    ReentrantLock a;
    public static void main(String[] args){
        a.lock();
        Thread t1 = new Thread(()->{
            synchronized (DirtyReadDemo.class)
            {
                number = 2;
                try {
                    Thread.sleep(3000); //延迟结束禁入区
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + ":" + number);
            }

        }, "t1");
        t1.start();

        Thread t2 = new Thread(()->{
            try {
                Thread.sleep(1000);//目的是让T1先执行执行
//                synchronized (DirtyReadDemo.class) {
//                    System.out.println(Thread.currentThread().getName() + ":" + number);
//                }
                System.out.println(Thread.currentThread().getName() + ":" + number);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t2");
        t2.start();
    }
}
